package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FastDFSUtil {

    static {
        try {
            //String filename = "\"D:\\\\IdeaDemo\\\\changgou\\\\changgou-parent\\\\changgou-service\\\\changgou-service-file\\\\src\\\\main\\\\resources\\\\application.yml\"";

            String filename = new ClassPathResource("fdfs_client.conf").getPath();

            ClientGlobal.init(filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //文件上传
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception {
        //附加参数
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author",fastDFSFile.getAuthor());

        TrackerServer trackerServer = getTrackerServer();

        StorageClient storageClient = getStorageClient(trackerServer);

        String[] uploads = storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), meta_list);

        return uploads;

    }

    //获取文件信息
    public static FileInfo getFile(String groupName, String remoteFileName) throws Exception {
        TrackerClient trackerClient = new TrackerClient();

        TrackerServer trackerServer = trackerClient.getConnection();

        StorageClient storageClient = new StorageClient(trackerServer, null);

        return storageClient.get_file_info(groupName,remoteFileName);
    }

    //文件下载
    public static InputStream downloadFile(String groupName, String remoteFileName) throws Exception {
        TrackerClient trackerClient = new TrackerClient();

        TrackerServer trackerServer = trackerClient.getConnection();

        StorageClient storageClient = new StorageClient(trackerServer, null);


        byte[] buffer = storageClient.download_file(groupName,remoteFileName);

        return new ByteArrayInputStream(buffer);
    }

    //删除文件

    public static void deleteFile(String groupName,String remoteFileName) throws Exception {
        TrackerClient trackerClient = new TrackerClient();

        TrackerServer trackerServer = trackerClient.getConnection();

        StorageClient storageClient = new StorageClient(trackerServer, null);
        storageClient.delete_file(groupName,remoteFileName);
    }


    //获取storages信息

    public static StorageServer getStorages() throws Exception {
        TrackerClient trackerClient = new TrackerClient();

        TrackerServer trackerServer = trackerClient.getConnection();

        return trackerClient.getStoreStorage(trackerServer);
    }

    //获取Storage的Ip和端口信息
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) throws Exception {
        TrackerClient trackerClient = new TrackerClient();

        TrackerServer trackerServer = trackerClient.getConnection();

        return trackerClient.getFetchStorages(trackerServer,groupName,remoteFileName);
    }

    //获取Tracker信息

    public static String getTrackerInfo() throws Exception {
        TrackerClient trackerClient = new TrackerClient();

        TrackerServer trackerServer = trackerClient.getConnection();

        String ip = trackerServer.getInetSocketAddress().getHostString();
        int tracker_http_port = ClientGlobal.getG_tracker_http_port();
        String url = "http://" + ip + ":" + tracker_http_port;
        return url;
    }

    public static TrackerServer getTrackerServer() throws IOException {
        TrackerClient trackerClient = new TrackerClient();

        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerServer;
    }

    public static StorageClient getStorageClient(TrackerServer trackerServer){
        StorageClient storageClient = new StorageClient(trackerServer, null);
        return storageClient;
    }

    public static void main(String[] args) throws Exception {
        //文件上传
        /*FileInfo fileInfo = getFile("group1", "M00/00/00/wKjThGBQXMSARKvEAAHx8IHW7iQ613.png");
        System.out.println(fileInfo.getSourceIpAddr());
        System.out.println(fileInfo.getFileSize());*/

        //文件下载
        //InputStream is = downloadFile("group1", "M00/00/00/wKjThGBQXMSARKvEAAHx8IHW7iQ613.png");

        //FileOutputStream os = new FileOutputStream("D:/1.jpg");

        //定义一个缓冲区
//        byte[] buffer = new byte[1024];
//        while(is.read(buffer)!=-1){
//            os.write(buffer);
//        }
//
//        os.flush();
//        os.close();
//        is.close();

        //文件删除
        //deleteFile("group1","M00/00/00/wKjThGBQZLGAUan4AAHx8IHW7iQ951.png");


        //获取Storage信息
//
//        StorageServer storageServer = getStorages();
//        System.out.println(storageServer.getStorePathIndex());
//        System.out.println(storageServer.getInetSocketAddress().getHostString());

        //获取Storage组的IP和端口信息
        /*ServerInfo[] groups = getServerInfo("group1", "M00/00/00/wKjThGBQZLGAUan4AAHx8IHW7iQ951.png");
        for (ServerInfo group : groups) {
            System.out.println(group.getIpAddr());
            System.out.println(group.getPort());
        }*/

        //获取Tracker的信息
        System.out.println(getTrackerInfo());
    }
}
